/*
SwpuStuManagement.java
西南石油大学 周永老师
微信：ZhouYswpu
QQ：1419797
提供界面和事件响应代码
2019年10月
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.sql.*;

//可以将数据封装到此对象中，也可以不用
class Student extends Object implements Serializable {
    String cNo; //学生学号
    String vName; //姓名
    String cSex; //性别
    String dBirth; //出生日期
    String cPhone; //电话号码
    String vEmail; //电子邮箱
}//end of class Student


//Java图形用户界面
public class SwpuStuManagement extends JFrame {
    JTabbedPane tabbedPane = new JTabbedPane(); //选项卡
    JPanel panelA, panelB, panelC;  //选项卡上的三个面板

    //面板A上的控件
    JLabel lblCno, lblName, lblSex, lblBirth, lblPhone, lblEmail;
    JComboBox cboSex;
    JTextField txtCno, txtName, txtBirth, txtPhone, txtEmail;
    JButton btnInsertData, buttonCancel;

    //面板B上的控件
    JLabel lblNoToDel;
    JTextField txtNoToDel;
    JButton btnNoToDel;

    //面板C上的控件
    JLabel lblNameToFind;
    JTextField txtNameToFind;
    JButton btnNameToFind;
    JTextArea areaShowResult;

    //构造方法
    public SwpuStuManagement() {
        super("SWPU学生信息管理(By 201331065432张三)"); //设置窗口标题栏文字
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tabbedPane = new JTabbedPane();
        panelA = new JPanel();
        panelB = new JPanel();
        panelC = new JPanel();
        tabbedPane.add("添加", panelA);
        tabbedPane.add("删除", panelB);
        tabbedPane.add("查询", panelC);

        //调用方法，往三个面板上添加组件
        addControlToPanelAdd();
        addControlToPanelDel();
        addControlToPanelQuery();


        //创建一个标签，用来显示文字，添加在North
        JLabel labelTitle = new JLabel(" 西南石油大学学生信息管理");
        labelTitle.setFont(new Font(null, Font.BOLD, 36)); //设置字体
        labelTitle.setForeground(new Color(60, 60, 160)); //设置颜色
        getContentPane().add("North", labelTitle);

        //添加选项卡组件到居中位置
        getContentPane().add("Center", tabbedPane);

        //给按钮注册事件侦听
        MyButtonListener listen = new MyButtonListener();
        btnInsertData.addActionListener(listen);
        btnNoToDel.addActionListener(listen);
        btnNameToFind.addActionListener(listen);
    } // end of constructor


    //添加控件到面板B（删除数据）上的方法
    public void addControlToPanelDel() {
        lblNoToDel = new JLabel("请输入要删除的学号：");
        txtNoToDel = new JTextField("201731061234", 15);
        btnNoToDel = new JButton("删除");
        panelB.add(lblNoToDel);
        panelB.add(txtNoToDel);
        panelB.add(btnNoToDel);
    }

    //添加控件到面板C（查询数据）上的方法
    public void addControlToPanelQuery() {
        lblNameToFind = new JLabel("请输入要查询的姓名(支持模糊查询)");
        ;
        txtNameToFind = new JTextField("刚", 6);
        btnNameToFind = new JButton("查询");
        areaShowResult = new JTextArea(20, 80); //20行，80列

        JPanel pTmp = new JPanel();
        pTmp.add(lblNameToFind);
        pTmp.add(txtNameToFind);
        pTmp.add(btnNameToFind);

        //面板C采用边界布局，上方是输入信息组件，下方是显示信息组件
        BorderLayout bl = new BorderLayout();
        panelC.setLayout(bl);

        //面板C的靠上组件pTmp，居中组件areaShowResult
        panelC.add("North", pTmp);
        panelC.add("Center", areaShowResult);
    }

    //添加控件到面板A上的方法
    public void addControlToPanelAdd() {
        lblCno = new JLabel("学号 *  ");
        lblName = new JLabel("姓名 *  "); //标签后面跟上1个星号提醒用户，这是必须输入的字段
        lblSex = new JLabel("性别 *  ");
        lblBirth = new JLabel("出生日期");
        lblPhone = new JLabel("电话号码");
        lblEmail = new JLabel("电子邮箱");

        txtCno = new JTextField("201731061234", 12);
        txtName = new JTextField("张三", 10);
        String sex[] = {"女", "男"};
        cboSex = new JComboBox(sex);
        //为了便于测试，文本框中给了一个默认数据
        txtBirth = new JTextField("1998-09-18", 10);
        txtPhone = new JTextField("028-83039876", 15);
        txtEmail = new JTextField("zhangs@yahoo.com.cn", 18);


        btnInsertData = new JButton("添加");
        buttonCancel = new JButton("清空");
        //对当前面板设置布局管理
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTHWEST;
        panelA.setLayout(gbl);
        //添加控件 第1行
        gbc.gridy = 1;
        gbc.gridx = 1;
        gbl.setConstraints(lblCno, gbc);
        panelA.add(lblCno);
        gbc.gridy = 1;
        gbc.gridx = 2;
        gbl.setConstraints(txtCno, gbc);
        panelA.add(txtCno);
        //添加控件 第2行
        gbc.gridy = 2;
        gbc.gridx = 1;
        gbl.setConstraints(lblName, gbc);
        panelA.add(lblName);
        gbc.gridy = 2;
        gbc.gridx = 2;
        gbl.setConstraints(txtName, gbc);
        panelA.add(txtName);
        //添加控件 第3行
        gbc.gridy = 3;
        gbc.gridx = 1;
        gbl.setConstraints(lblSex, gbc);
        panelA.add(lblSex);
        gbc.gridy = 3;
        gbc.gridx = 2;
        gbl.setConstraints(cboSex, gbc);
        panelA.add(cboSex);
        //添加控件 第4行
        gbc.gridy = 4;
        gbc.gridx = 1;
        gbl.setConstraints(lblBirth, gbc);
        panelA.add(lblBirth);
        gbc.gridy = 4;
        gbc.gridx = 2;
        gbl.setConstraints(txtBirth, gbc);
        panelA.add(txtBirth);
        //添加控件 第5行
        gbc.gridy = 5;
        gbc.gridx = 1;
        gbl.setConstraints(lblPhone, gbc);
        panelA.add(lblPhone);
        gbc.gridy = 5;
        gbc.gridx = 2;
        gbl.setConstraints(txtPhone, gbc);
        panelA.add(txtPhone);
        //添加控件 第6行
        gbc.gridy = 6;
        gbc.gridx = 1;
        gbl.setConstraints(lblEmail, gbc);
        panelA.add(lblEmail);
        gbc.gridy = 6;
        gbc.gridx = 2;
        gbl.setConstraints(txtEmail, gbc);
        panelA.add(txtEmail);


        //添加控件 第7行
        gbc.gridy = 7;
        gbc.gridx = 3;
        gbl.setConstraints(btnInsertData, gbc);
        panelA.add(btnInsertData);
        gbc.gridy = 7;
        gbc.gridx = 4;
        gbl.setConstraints(buttonCancel, gbc);
        panelA.add(buttonCancel);
    }

    //显示提示信息的方法
    private void showMsg(String sArgs) {
        JOptionPane.showMessageDialog(SwpuStuManagement.this, sArgs);
    }//end of showMsg


    class MyButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            //根据事件源是不同按钮，调用不同的方法
            if (evt.getSource() == btnInsertData) insertData();
            if (evt.getSource() == btnNoToDel) deleteData();
            if (evt.getSource() == btnNameToFind) selectData();
        }
    } // end of class MyButtonListener

    public static void main(String args[]) {
        SwpuStuManagement obj = new SwpuStuManagement();
        obj.setSize(640, 480);
        obj.setVisible(true);
    }//end of main

    //添加数据对应的代码封装在这里
    private void insertData() {
        String sNo = txtCno.getText();
        String sName = txtName.getText();
        String sSex = (String) cboSex.getSelectedItem();
        String sBirth = txtBirth.getText();
        String sPhone = txtPhone.getText();
        String sEmail = txtEmail.getText();

        String sSapce = "    ";//四个空格
        String sTmp = sNo + sSapce + sName + sSapce + sSex + sSapce + sBirth + sSapce + sPhone + sSapce + sEmail;
        showMsg(sTmp);
    }//end of insertData()


    //删除数据对应的代码封装在这里
    private void deleteData() {
        String sNo = txtNoToDel.getText();
        showMsg("您要删除的学号是：" + sNo);
    }//end of deleteData()

    //查询数据对应的代码封装在这里
    private void selectData() {
        String sName = txtNameToFind.getText();
        showMsg("您要模糊查询的姓名：" + sName);
    }//end of selectData()

}//end of class SwpuStuManagement